﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;

public partial class Add_Brand:System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {

            if (Request.QueryString["Id"] != null)
            {
                lblHeader.Text = "Edit Brand";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add Brand";
            }
        }
      
    }
    #endregion

    #region save cancel and clear
    protected void Save(object sender, EventArgs e)
    {
        Brand brand = new Brand();
        Brand getBrand = new Brand();
        try
        {
            if (string.IsNullOrEmpty(txtBrandName.Text.Trim()))
            {
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('* are mendatory fields.')", true);
                return;

            }

            brand = new Brand(_dataObject);
            BuildObject(brand);

            //check brand name is exist or not
            getBrand = new Brand(_dataObject);
            BuildBrandObject(getBrand);

            if (Request.QueryString["Id"] != null)
            {
                if (getBrand.Id != Convert.ToInt32(Request.QueryString["Id"]) && getBrand.Id != 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Brand Name is already exist..')", true);
                    return;
                }
                brand.Id = Convert.ToInt32(Request.QueryString["Id"]);
                brand.UpdatedDate = System.DateTime.Now;
                brand.IsDeleted = false;
                brand.Update();
                Response.Redirect("Brand-List.aspx");
            }
            else
            {
                if (getBrand.Id > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Brand Name is already exist..')", true);
                    return;
                }
                BuildObject(brand);
                brand.CreatedDate = System.DateTime.Now;
                brand.Create();
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "callAlert('brand is added successfully');", true);
            }
            ClearControls();

        }
        catch (Exception ex)
        {
        }
    }

    private void ClearControls()
    {
        txtBrandName.Text = string.Empty;

    }
    #endregion


    #region binddata
    private void BindData()
    {
        Brand brand = new Brand();
        brand = new Brand(_dataObject);
        brand.Id = Convert.ToInt32(Request.QueryString["Id"]);
        brand.GetBrandById();
        txtBrandName.Text = brand.Name;
    }
    private void BuildObject(Brand brand)
    {
        brand.Name = txtBrandName.Text;
    }
    private void BuildBrandObject(Brand brand)
    {
        brand.Name = txtBrandName.Text;
        brand.GetBrandByName();
    }

    #endregion
}